import { Injectable } from '@angular/core';
import { SettingsService, _HttpClient } from '@delon/theme';
import { Observable } from 'rxjs';
import { TENANT_KEY_PREFIX } from 'src/app/shared/constant/common-constant';

import { Api } from '../api/api';

@Injectable({
  providedIn: 'root'
})
export class PublicService {
  constructor(private http: _HttpClient, public settings: SettingsService) {}

  clearCurrentTenant() {
    localStorage.removeItem(`${TENANT_KEY_PREFIX}${this.settings.user['id']}`);
  }

  setCurrentTenant(value: string) {
    localStorage.setItem(`${TENANT_KEY_PREFIX}${this.settings.user['id']}`, value);
  }

  getCurrentTenant(): any | null {
    const tenantInfoStr = localStorage.getItem(`${TENANT_KEY_PREFIX}${this.settings.user['id']}`) || null;
    if (!tenantInfoStr) {
      return null;
    }
    const firmInfo = JSON.parse(tenantInfoStr);
    return firmInfo;
  }
  /**
   * 获取字典列表
   *
   * @param key 字典键值
   */
  queryDicByIndex(key: string): Observable<any> {
    return this.http.get(`${Api.BaseDictApi}index/key/${key}`);
  }
}
